//
//  GProgressIndicatorCell.m
//  Genesis
//
//  Created by Kristoffer Andersen on 14/03/09.
//  Copyright 2009 Kristoffer Andersen. All rights reserved.
//
//  This file is part of Genesis.
//  
//  Genesis is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  Genesis is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with Genesis.  If not, see <http://www.gnu.org/licenses/>.

#import "GProgressIndicatorCell.h"


@implementation GProgressIndicatorCell

-(id)init
{
	self = [super init];
	//NSLog(@"GProgress init called...");
	connectionsProgressIndicators = [[NSMutableArray alloc] init];
	stdProgress = [[NSProgressIndicator alloc] init];
	
	greenView = [[NSImageView alloc] init];
	redView = [[NSImageView alloc] init];
	[greenView setImageScaling:NSScaleProportionally];
	[redView setImageScaling:NSScaleProportionally];
	[greenView setImageFrameStyle:NSImageFrameNone];
	[redView setImageFrameStyle:NSImageFrameNone];
	
	[stdProgress setStyle:NSProgressIndicatorSpinningStyle];
	[stdProgress setControlSize:NSSmallControlSize];
	//[stdProgress startAnimation:nil];
	[stdProgress setHidden:NO];
	[stdProgress setDisplayedWhenStopped:YES];

	
	//Read images
	NSImage *green = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForImageResource:@"feeder-green.png"]];
	NSImage *red = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForImageResource:@"feeder-red.png"]];
	[greenView setImage:green];
	[redView setImage:red];
	
	return self;
}


//  Methods by Brian Dunagan on 12/6/08.
//  Copyright 2008 bdunagan.com. All rights reserved.
- (void)drawInteriorWithFrame:(NSRect)aRect inView:(NSView *)controlView
{
	[super drawInteriorWithFrame:aRect inView:controlView];
	
	NSRect progressRect = NSMakeRect(aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);
	//progressRect.origin.x += 3;
	//progressRect.origin.y += 2;
	if (status == 1) {
		[stdProgress setFrame:progressRect];
		//NSLog(@"GProgress: DrawInterior called Progress!");
		[stdProgress sizeToFit];
	}
	else if (status == 2 || status == 0) {
		[greenView setFrame:progressRect];//NSMakeRect(progressRect.origin.x,progressRect.origin.y,20.0,20.0)];
		//NSLog(@"GProgress: DrawInterior called Green!");
		//[greenView sizeToFit];
	}
	else {
		[redView setFrame:progressRect];
		//[redView sizeToFit];
	}
	
}

- (void)setProgressAtIndex:(int)index
{	
	[stdProgress release];
	stdProgress = [connectionsProgressIndicators objectAtIndex:index];
	[stdProgress retain];
	[stdProgress sizeToFit];
}

-(NSProgressIndicator *)addIndicator
{
	progressIndicator = [[NSProgressIndicator alloc] init];
	[progressIndicator setStyle:NSProgressIndicatorSpinningStyle];
	[progressIndicator setControlSize:NSSmallControlSize];
	//[progressIndicator startAnimation:nil];
	[progressIndicator setHidden:NO];
	[progressIndicator setDisplayedWhenStopped:NO];
	[connectionsProgressIndicators addObject:progressIndicator];
	[progressIndicator autorelease];
	//NSLog(@"GProgress added indicator...");
	return progressIndicator;
}
-(NSProgressIndicator *)indicatorAtIndex:(int)index
{
	status = 1;
	[greenView setFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
	[redView setFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
	return [connectionsProgressIndicators objectAtIndex:index];
}

-(NSImageView *)isConnected
{
	[redView setFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
	//NSLog(@"GreenRect: %f,%f,%f,%f",[greenView frame].origin.x,[greenView frame].origin.y,[greenView frame].size.height,[greenView frame].size.width);
	status = 2;
	return greenView;
}
-(NSImageView *)isInterrupted
{
	[greenView setFrame:NSMakeRect(0.0, 0.0, 0.0, 0.0)];
	status = 3;
	return redView;
}

@end
